package org.kazuhiko.rpg.server.model;

import java.util.HashSet;
import java.util.Set;

import org.kazuhiko.rpg.server.net.ActionSender;
import org.kazuhiko.rpg.server.net.RPGSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Player extends Entity
{
	private static final Logger logger = LoggerFactory.getLogger( Player.class );
	
	private final RPGSession session;
	
	private World world;
	
	private Set<Entity> localEntities; //All the other players
	
	public Player( RPGSession session, String name )
	{
		this.session = session;
		localEntities = new HashSet<Entity>();
		setName( name );
	}
	
	public RPGSession getSession()
	{
		return session;
	}
	
	public ActionSender getActionSender()
	{
		return session.getActionSender();
	}
	
	public void teleport( Position position, Rotation rotation )
	{
		setPosition( position );
		setRotation( rotation );
		session.getActionSender().sendTeleport( position, rotation );
	}
	
	public void moveToWorld( World world )
	{
		logger.debug( "Moving player {} to world {}", this, world );
		
		if( this.world != null )
		{
			this.world.removePlayer( this );
		}
		
		assert( world != null );
		setWorld( world );
		world.addPlayer( this );
	}
	
	public void setWorld( World world )
	{
		this.world = world;
	}
	
	public World getWorld()
	{
		return world;
	}
	
	//Override means that we override the function that our super class has
	@Override
	public boolean equals( Object other )
	{
		//We check here if the other object is an Player object
		if( other instanceof Player )
		{
			Player player = ( Player ) other; //We cast the other object to a player object
			return getName().equals( player.getName() ); // now we return if the names are the same
		}
		
		return false;
	}
	
	@Override
	public String toString()
	{
		return getName();

	}
	
	public Set<Entity> getLocalEntities()
	{
		return localEntities;
	}
}
